package net.sourceforge.krama.xml;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import net.sourceforge.krama.introspection.TMethod;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;


public class TXMLLoader {

	private static TXMLLoader instance = null;
	
	/**
	 * Singleton instance
	 * @return TXMLLoader
	 */
	public static TXMLLoader getInstance() {

		if (instance == null) {

			synchronized (TXMLLoader.class) {
				if (instance == null) {
					instance = new TXMLLoader();
				}
			}
		}
		return instance;
	}
	/**
	 * Obtains a SaxBuilder Document
	 * @param configkrama
	 * @return Document
	 */
	public Document openkramaConfig(String configkrama) {

		SAXBuilder sxb = new SAXBuilder();
		Document document = null;
		try {

			document = sxb.build(new File(configkrama));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return document;
	}
	
	/**
	 * This method allows to check if our informations provided on Ajax call are correct
	 * @param document
	 * @param nClass
	 * @param nMethod
	 * @param nParam
	 * @return boolean
	 */
	public static boolean checkAjaxInformation(Document document, String nClass,
			String nMethod, Object[] nParam){
		
		Element racine = document.getRootElement();
		Element krama_ajax_call = racine.getChild("krama-ajax-call");
		List krama_MethodList = krama_ajax_call.getChildren("krama-class");
		
		return false;
	}
	
	/**
	 * This method return a kind of class and his method with parameter's
	 * @param document
	 * @param nClass
	 * @param nMethod
	 * @param nParam
	 * @return TMethod
	 */
	public static TMethod parsingkramaConfigMethod(Document document, String nClass,
			String nMethod, Object[] nParam) {

		Element racine = document.getRootElement();
		TMethod tMethod = new TMethod();
		Element krama_ajax_call = racine.getChild("krama-ajax-call");
		List krama_MethodList = krama_ajax_call.getChildren("krama-class");

		for (int i = 0; i < krama_MethodList.size(); i++) {

			Element krama_class = ((Element) krama_MethodList.get(i));
			if (krama_class.getAttributeValue("name").equals(nClass)) {
				Element krama_method = krama_class.getChild("krama-method");
				if (krama_method.getAttributeValue("name").equals(nMethod)) {
					List krama_paramList = krama_method.getChildren("krama-param");
					if (nParam.length == krama_paramList.size()) {

						tMethod.setClasse(krama_class.getAttributeValue("class"));
						tMethod.setMethode(krama_method
								.getAttributeValue("method"));
						List<String> params = new ArrayList<String>(
								nParam.length);
						for (int j = 0; j < nParam.length; j++) {

							params.add(((Element) krama_paramList.get(j))
									.getAttributeValue("type"));
						}
						tMethod.setParams(params);
					} else {
						break;
					}
				}
			}

		}
		return tMethod;
	}
	/**
	 * Obtains a value of JSPPage
	 * @param document
	 * @return String
	 */
	public String parsingkramaConfigJSPPageName(Document document) {
		// TODO Auto-generated method stub
		Element racine = document.getRootElement();
		return racine.getChild("krama-jsp").getAttributeValue("location");
	}

	/**
	 * Obtains a value of JSP Key which allows redirect a Ajax Servlet in correct Web page
	 * @param document
	 * @return String
	 */
	public String parsingkramaConfigJSPKey(Document document) {
		// TODO Auto-generated method stub
		Element racine = document.getRootElement();
		return racine.getChild("krama-jsp").getAttributeValue("key");
	}
	
	/**
	 * Obtains a value of CSS file
	 * @param document
	 * @return String
	 */
	public String parsingkramaConfigCSSPageName(Document document) {
		// TODO Auto-generated method stub
		Element racine = document.getRootElement();
		return racine.getChild("krama-css").getAttributeValue("location");
	}
	
	/**
	 * Obtains a JS file
	 * @param document
	 * @return String
	 */
	public String parsingkramaConfigJSPageName(Document document) {
		// TODO Auto-generated method stub
		Element racine = document.getRootElement();
		return racine.getChild("krama-js").getAttributeValue("location");
	}
	
	/**
	 * This method allows to get a value of JSP page
	 * @param document
	 * @param view
	 * @return String
	 */
	public String parsingkramaConfigViewName(Document document, String view) {
		// TODO Auto-generated method stub
		String value = null;
		
		Element racine = document.getRootElement();
		Element krama_Views = racine.getChild("krama-views");
		List krama_ViewList = krama_Views.getChildren("krama-view");
		for (int i = 0; i < krama_ViewList.size(); i++) {
			Element krama_view = ((Element)krama_ViewList.get(i));
			if (krama_view.getAttributeValue("name").equals(view)) {
				value = krama_view.getAttributeValue("class");
				break;
			}
		}
		return value;
	}

}
